#include "lcd_app.h"
#include "picture.h"
#include "general_config.h"
#include "stdio.h"
#include "Beep.h"
char ShowTemp[32];
double BMI;
// settingflag SettingFlag;
#pragma diag_suppress 77, 550, 1294, 1295
#pragma diag_suppress 870

// extern bool system_started;
PressureState currentState = STATE_INIT;

/**********************************************/
//当重新赋值Flag.PageNO时，必然page_hight == Flag.PageNO
void show_setting_para()
{
	static uint8_t old_hight, old_weight, old_wavelenth, old_turnlenth, old_turnmode, old_mode;
	//身高
	if (page_hight == Flag.PageNO)//身高显示
	{
		if (Flag.old_pageNO != Flag.PageNO)
		{
			TFTLCD_Draw_RLE_Bitmap(0, 0, (const GUI_BITMAP *)&bmhight_new);//身高界面背景
		}
		if ((SettingPARA.hight != old_hight) || (Flag.old_pageNO != Flag.PageNO))
		{
			sprintf(ShowTemp, "%3d ", SettingPARA.hight);
			TFTLCD_Str(shengao_x, shengao_y, MY_BLUE, DISE, ShowTemp, &weiruancu62);//身高界面数字
			old_hight = SettingPARA.hight;
		}
		Flag.old_pageNO = Flag.PageNO;//复位标志位
	}
	//体重
	else if (page_weight == Flag.PageNO)
	{
		if (Flag.old_pageNO != Flag.PageNO)
		{
			TFTLCD_Draw_RLE_Bitmap(0, 0, (const GUI_BITMAP *)&bmweight_new);
		}
		if ((SettingPARA.weight != old_weight) || (Flag.old_pageNO != Flag.PageNO))
		{
			sprintf(ShowTemp, "%3d ", SettingPARA.weight);
			TFTLCD_Str(tizhong_x, tizhong_y, MY_BLUE, DISE, ShowTemp, &weiruancu62);
			old_weight = SettingPARA.weight;
		}
		Flag.old_pageNO = Flag.PageNO;
	}
	//波动

	else if (page_wave == Flag.PageNO)
	{
		if (Flag.old_pageNO != Flag.PageNO)
		{
			TFTLCD_Draw_RLE_Bitmap(0, 0, (const GUI_BITMAP *)&bmwave_new);//波动模式界面
		}
		if ((SettingPARA.WaveCycle_lenth != old_wavelenth) || (Flag.old_pageNO != Flag.PageNO))
		{
			sprintf(ShowTemp, "%3d ", SettingPARA.WaveCycle_lenth);
			TFTLCD_Str(116, 76, MY_BLUE, DISE, ShowTemp, &weiruancu62);//波动数字
			old_wavelenth = SettingPARA.WaveCycle_lenth;
		}
		Flag.old_pageNO = Flag.PageNO;
	}
}

/***************************adc_debug检测*******************/
void show_list()//进度条
{
	//	TFTLCD_Draw_RLE_Bitmap(0, 0,(const GUI_BITMAP *)&bmbeijing);
	//	TFTLCD_ShowPicture(moshi_x, moshi_y,gImage_jingtai, 50, 25);
	//	TFTLCD_Str(fanshen_x,fanshen_y,MY_ORANGE,BLACK,"TURN_OFF  ",&Arial_21);
	//	LCD_ShowChinese(moshi_x,moshi_y,"模式",WHITE,BLACK,16,0);

	TFTLCD_Draw_RLE_Bitmap(0, 0, (const GUI_BITMAP *)&bm01);

	//	TFTLCD_Draw_RLE_Bitmap(23, 103,(const GUI_BITMAP *)&bmTableview_Icon_11);
	//	TFTLCD_Str(140,65,0x3c39,DISE,"170",&weiruancu62);
	//	TFTLCD_Draw_RLE_Bitmap(0, 0,(const GUI_BITMAP *)&bm33);
}

/***************************adc_debug检测*******************/
extern char Web_Control_Flag;
void show_info()// LCD显示
{
	static unsigned char old_mqtt_conected, old_warn_info, old_weight, old_hight, old_mode, old_turnmode, old_WaveCycle_lenth, old_TurnCycle_lenth,
		old_inbed, old_route_conected, old_set_password, old_PressSensOn, old_foot_gravity, old_haunch_gravity, old_should_gravity, old_head_gravity;
	static unsigned short old_pressure, old_cycle_LeftTime, old_turn_LeftTime;
	static double old_BMI, old_threshold;
	static uint32_t old_nowave_keeptime, old_turn_keeptime;
	unsigned char hour, min, second;

	show_setting_para();
	/*************锁屏提示*******************/
	if (page_lock == Flag.PageNO)
	{
		if (Flag.old_pageNO != Flag.PageNO)
			TFTLCD_Draw_RLE_Bitmap(0, 0, (const GUI_BITMAP *)&bmlock_new);
        //TFTLCD_Draw_RLE_Bitmap(-1,0, (const GUI_BITMAP *)&bm22223);
		Flag.old_pageNO = Flag.PageNO;
	}

	/**********压力过低弹窗界面**********/
	if (page_low_pressure == Flag.PageNO)
	{
		if (Flag.old_pageNO != Flag.PageNO)
		{
			TFTLCD_Draw_RLE_Bitmap(0, 0, (const GUI_BITMAP *)&bmMedium_priority_map);//压力过低弹窗界面(中优先级)
			// TFTLCD_FillRect(289, 13, bmdiya_warn.Width, bmdiya_warn.Height, WHITE);
			// TFTLCD_Fill(255, 12, 276, 27, DISE);
		}
		Flag.old_pageNO = Flag.PageNO;
	}

	/*************主界面*******************/
	if (page_home == Flag.PageNO)
	{
		// warn_button = 2 就是代表正常运行，没有处于低压报警的情况
		warn_button = 2 ;
		// stop_alarm();
		if (Flag.old_pageNO != Flag.PageNO)
			TFTLCD_Draw_RLE_Bitmap(0, 0, (const GUI_BITMAP *)&bmMain_page);

		/*************连接状态*******************/
		if ((Flag.mqtt_conected != old_mqtt_conected) || (Flag.old_pageNO != Flag.PageNO)) // mqtt
		{
			if (Flag.mqtt_conected)
				TFTLCD_Draw_RLE_Bitmap(255, 12, (const GUI_BITMAP *)&bmwifi); // wifi图标
			else
				TFTLCD_Fill(255, 12, 276, 27, DISE);

			old_mqtt_conected = Flag.mqtt_conected;
		}

		if (Flag.set_password != old_set_password) //配网提示
		{
			if (1 == Flag.set_password)
				LCD_ShowChinese(SetPass_x, SetPass_y, "配网中    ", MY_ORANGE, DISE, 16, 0);
			else if (2 == Flag.set_password)
				LCD_ShowChinese(SetPass_x, SetPass_y, "配网成功", MY_BLUE, DISE, 16, 0);

			old_set_password = Flag.set_password;
		}
		/*************预警信息*******************/

		if ((CurrentPARA.warn_info != old_warn_info) || (Flag.old_pageNO != Flag.PageNO))
		{
			if (currentState == STATE_NORMAL)
			{
				if ((2 == CurrentPARA.warn_info)) //低压  286 12
				{//286 横坐标  12纵坐标 
					TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmdiya_warn);//显示 红色字“低压”
					// TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmzhengchang_warn);//显示 黑色字“正常”
				}
				else
				{
					TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmzhengchang_warn);//显示 黑色字“正常”
					// TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmdiya_warn);//显示 红色字“低压”
				}
			}

			old_warn_info = CurrentPARA.warn_info;
		}

		/*********************波动模式*********************/
		if (MODE_WAVE == CurrentPARA.mode) //波动模式下
		{
			/*************模式*******************/
			if ((CurrentPARA.mode != old_mode) || (Flag.old_pageNO != Flag.PageNO))
			{
				TFTLCD_Draw_RLE_Bitmap(moshi_x, moshi_y, (const GUI_BITMAP *)&bmbodong_home);
			}

			/*************波动交替剩余时间*******************/
			if ((cycle_LeftTime != old_cycle_LeftTime) || (Flag.old_pageNO != Flag.PageNO))
			{
				//			if(cycle_LeftTime>(WaveLenth_max*60)) min=WaveLenth_max;
				hour = cycle_LeftTime / 3600;
				min = cycle_LeftTime % 3600 / 60;
				second = cycle_LeftTime % 3600 % 60;
				sprintf(ShowTemp, "%02d:%02d:%02d", hour, min, second);
				TFTLCD_Str(bodongleftime_x, bodongleftime_y, MY_ORANGE, MY_BLUE, ShowTemp, &weiruancu27);//原
				// TFTLCD_Str(fanshenleftime_x, fanshenleftime_y, MY_ORANGE, MY_BLUE, ShowTemp, &weiruancu27);

				old_cycle_LeftTime = cycle_LeftTime;
			}
			/*************波动周期*******************/
			if (CurrentPARA.WaveCycle_lenth != old_WaveCycle_lenth || (Flag.old_pageNO != Flag.PageNO) || Web_Control_Flag)
			{
				//			SettingFlag.WaveCycle_lenth=0;
				Web_Control_Flag = 1;
				sprintf(ShowTemp, " /  %02d:%02d", CurrentPARA.WaveCycle_lenth / 60, CurrentPARA.WaveCycle_lenth % 60);
				TFTLCD_Str(bodongzhouqi_x, bodongzhouqi_y, WHITE, MY_BLUE, ShowTemp, &weiruancu27);//原
				// TFTLCD_Str(fanshenzhouqi_x, fanshenzhouqi_y, WHITE, MY_BLUE, ShowTemp, &weiruancu27);
				old_WaveCycle_lenth = CurrentPARA.WaveCycle_lenth;
			}
			old_mode = CurrentPARA.mode;
		}

		else
		{
			// char emptyStr[] = "              ";  // 空白字符串，长度比最大时间字符串长一点
			if (MODE_NURSE == CurrentPARA.mode) //护理模式   主界面护理模式字符显示
			{
				Flag.PageNO = page_home;
				TFTLCD_Draw_RLE_Bitmap(moshi_x, moshi_y, (const GUI_BITMAP *)&bmhuli_home);
			}
			else// if (MODE_STATIC == CurrentPARA.mode) //静态模式    主界面静态模式字符显示
			{
				Flag.PageNO = page_home;
				TFTLCD_Draw_RLE_Bitmap(moshi_x, moshi_y, (const GUI_BITMAP *)&bmjingtai_home);
			}

			if (old_nowave_keeptime != nowave_keeptime)
			{	
				hour = nowave_keeptime / 3600;
				min = nowave_keeptime % 3600 / 60;
				second = nowave_keeptime % 3600 % 60;
				sprintf(ShowTemp, "%02d:%02d:%02d", hour, min, second);

				// TFTLCD_Fill(bodongleftime_x, bodongleftime_y, bodongleftime_x + 180, bodongleftime_y + 24, MY_BLUE);

				TFTLCD_Str(160, 164, MY_ORANGE, MY_BLUE, ShowTemp, &weiruancu27);   //主界面静态模式显示

				old_nowave_keeptime = nowave_keeptime;//时间标志位复位
			}
			old_mode = CurrentPARA.mode;

			// char emptyStr[] = "              ";  // 空白字符串，长度比最大时间字符串长一点
			// if (MODE_NURSE == CurrentPARA.mode) //护理模式   主界面护理模式字符显示
			// {
			// 	Flag.PageNO = page_home;
			// 	TFTLCD_Draw_RLE_Bitmap(moshi_x, moshi_y, (const GUI_BITMAP *)&bmhuli_home);
			// }
			// else// if (MODE_STATIC == CurrentPARA.mode) //静态模式    主界面静态模式字符显示
			// {
			// 	Flag.PageNO = page_home;
			// 	TFTLCD_Draw_RLE_Bitmap(moshi_x, moshi_y, (const GUI_BITMAP *)&bmjingtai_home);
			// }

			// if (old_nowave_keeptime != nowave_keeptime)
			// {
			// 	hour = nowave_keeptime / 3600;
			// 	min = nowave_keeptime % 3600 / 60;
			// 	second = nowave_keeptime % 3600 % 60;
			// 	sprintf(ShowTemp, "%02d:%02d:%02d", hour, min, second);

			// 	TFTLCD_Str(bodongleftime_x, bodongleftime_y, MY_BLUE, MY_BLUE, emptyStr, &weiruancu27);
			// 	TFTLCD_Str(bodongzhouqi_x, bodongzhouqi_y, MY_BLUE, MY_BLUE, emptyStr, &weiruancu27);

			// 	TFTLCD_Str(160, 164, MY_ORANGE, MY_BLUE, ShowTemp, &weiruancu27);   //主界面静态模式显示
			// 	// TFTLCD_Str(bodongzhouqi_x, bodongzhouqi_y, MY_BLUE, MY_BLUE, ShowTemp, &weiruancu27);//原

        	// 	//TFTLCD_Str(160, 129, MY_BLUE, RED, ShowTemp, &weiruancu27);   //主界面静态模式显示

			// 	old_nowave_keeptime = nowave_keeptime;//时间标志位复位
			// }
			// old_mode = CurrentPARA.mode;
			// Flag.PageNO = page_home;
		}

		/*************体重*******************/
		if (CurrentPARA.weight != old_weight || (Flag.old_pageNO != Flag.PageNO))
		{
			//		SettingFlag.weight=0;
			sprintf(ShowTemp, "%3d", CurrentPARA.weight);
			TFTLCD_Str(64, 12, BLACK, DISE, ShowTemp, &Arial_18);//主界面正上方 体重图案
			old_weight = CurrentPARA.weight;
		}

		/*************身高*******************/
		if (CurrentPARA.hight != old_hight || (Flag.old_pageNO != Flag.PageNO))
		{
			//		SettingFlag.hight=0;
			sprintf(ShowTemp, "%3d", CurrentPARA.hight);
			TFTLCD_Str(13, 12, BLACK, DISE, ShowTemp, &Arial_18);//主界面正上方 身高图案
			//TFTLCD_Str(13, 12, RED, DISE, ShowTemp, &Arial_18);//主界面正上方 身高图案
			old_hight = CurrentPARA.hight;
		}
		/*************BMI************************/
		//	if(BMI!=old_BMI)
		//	{
		//		sprintf(ShowTemp,"%6.3f ",BMI);
		//		TFTLCD_Str(BMI_x,BMI_y+18,MY_ORANGE,BLACK,ShowTemp,&Arial_18);
		//		old_BMI=BMI;
		//	}
		/******************预期气压*******************/
		//	if(threshold!=old_threshold)
		//	{
		//		sprintf(ShowTemp,"%-4.1f kpa ",threshold/1000.0);
		//		TFTLCD_Str(yuqiyali_x,yuqiyali_y+18,MY_ORANGE,BLACK,ShowTemp,&Arial_18);
		//		old_threshold=threshold;
		//	}
		/*************实时气压*******************/
		if (CurrentPARA.air_pressure != old_pressure || (Flag.old_pageNO != Flag.PageNO))
		{
			sprintf(ShowTemp, "%4.1f ", (float)CurrentPARA.air_pressure / 1000.0);
			TFTLCD_Str(shijiyali_x, shijiyali_y, BLACK, DISE, ShowTemp, &Arial_18);   //主界面正上方气压数字显示

			old_pressure = CurrentPARA.air_pressure;
		}

		/*************在离床*******************/
		//		if((CurrentPARA.inbed!=old_inbed)||(Flag.old_pageNO!=Flag.PageNO))
		//		{
		//			if(CurrentPARA.inbed)
		//				LCD_ShowChinese(lichuang_x, lichuang_y,"在床",MY_ORANGE,BLACK,16,0);
		//			else
		//				LCD_ShowChinese(lichuang_x, lichuang_y,"离床",MY_ORANGE,BLACK,16,0);
		//			old_inbed=CurrentPARA.inbed;
		//		}

		/***********************压力垫显示*******************/

		if (1 == Flag.PressSensOn) //压力垫不同情况下显示
		{
			if ((old_PressSensOn != Flag.PressSensOn) || (old_head_gravity != warn_para.head_gravity) || (Flag.old_pageNO != Flag.PageNO)) //头部
			{
				//X-2,Y+5
				if (1 == warn_para.head_gravity)
					TFTLCD_Draw_RLE_Bitmap(24, 82, (const GUI_BITMAP *)&bmyichang_home);
				else
					TFTLCD_Draw_RLE_Bitmap(24, 82, (const GUI_BITMAP *)&bmzhengchang_home);

				old_head_gravity = warn_para.head_gravity;
			}
			if ((old_PressSensOn != Flag.PressSensOn) || (old_should_gravity != warn_para.should_gravity) || (Flag.old_pageNO != Flag.PageNO)) //肩部
			{
				if (1 == warn_para.should_gravity)
					TFTLCD_Draw_RLE_Bitmap(102, 82, (const GUI_BITMAP *)&bmyichang_home);
				else
					TFTLCD_Draw_RLE_Bitmap(102, 82, (const GUI_BITMAP *)&bmzhengchang_home);

				old_should_gravity = warn_para.should_gravity;
			}
			if ((old_PressSensOn != Flag.PressSensOn) || (old_haunch_gravity != warn_para.haunch_gravity) || (Flag.old_pageNO != Flag.PageNO)) //臀部
			{
				if (1 == warn_para.haunch_gravity)
					TFTLCD_Draw_RLE_Bitmap(182, 82, (const GUI_BITMAP *)&bmyichang_home);
				else
					TFTLCD_Draw_RLE_Bitmap(182, 82, (const GUI_BITMAP *)&bmzhengchang_home);

				old_haunch_gravity = warn_para.haunch_gravity;
			}
			if ((old_PressSensOn != Flag.PressSensOn) || (old_foot_gravity != warn_para.foot_gravity) || (Flag.old_pageNO != Flag.PageNO)) //脚部
			{
				if (1 == warn_para.foot_gravity)
					TFTLCD_Draw_RLE_Bitmap(259, 82, (const GUI_BITMAP *)&bmyichang_home);
				else
					TFTLCD_Draw_RLE_Bitmap(259, 82, (const GUI_BITMAP *)&bmzhengchang_home);

				old_foot_gravity = warn_para.foot_gravity;
			}

			old_PressSensOn = Flag.PressSensOn;
		}
		else
		{ //没插上压力垫

			if ((old_PressSensOn != Flag.PressSensOn) || (Flag.old_pageNO != Flag.PageNO))
			{
				if (old_PressSensOn != Flag.PressSensOn)
				{
					TFTLCD_Fill(26, 77, 61, 94, MY_ORANGE);
					TFTLCD_Fill(103, 77, 138, 94, MY_ORANGE);
					TFTLCD_Fill(182, 77, 217, 94, MY_ORANGE);
					TFTLCD_Fill(259, 77, 294, 94, MY_ORANGE);
				}

				TFTLCD_Draw_RLE_Bitmap(40, 84, (const GUI_BITMAP *)&bmbhh);
				TFTLCD_Draw_RLE_Bitmap(117, 84, (const GUI_BITMAP *)&bmbhh);
				TFTLCD_Draw_RLE_Bitmap(195, 84, (const GUI_BITMAP *)&bmbhh);
				TFTLCD_Draw_RLE_Bitmap(272, 84, (const GUI_BITMAP *)&bmbhh);
			}
			old_PressSensOn = Flag.PressSensOn;
		}

		Flag.old_pageNO = Flag.PageNO;
	}
}

void Sound_off_icon()
{
	if (page_low_pressure == Flag.PageNO)
	{
		if (warn_button == 4)//声音清除按键按下后，在压力过低界面显示声音清除图标
		{
			TFTLCD_Draw_RLE_Bitmap(233, 5, (const GUI_BITMAP *)&bmSound_removal);//位置还没确定
		}
		else
		{
			TFTLCD_Fill(233, 5, 318, 82, WHITE);//否则就填充白色
		}
	}
}

// 状态机处理函数
void pressureStateMachine()
{
	if (page_home == Flag.PageNO)
	{
		switch (currentState)
		{
			case STATE_INIT://开机状态
				// 开机显示低压
				TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmdiya_warn);

				if (CurrentPARA.air_pressure > (threshold - warn_deviation))
				{
					currentState = STATE_NORMAL;
					TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmzhengchang_warn);
				}
				break;

			case STATE_NORMAL://正常
				if (CurrentPARA.air_pressure < (threshold - warn_deviation))
				{
					if (warn_para.LowPress_time == warn_LowPress_time)
					{
						// currentState = STATE_LOW_PRESSURE;
						// TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmdiya_warn);
						CurrentPARA.warn_info = (CurrentPARA.warn_info & 0xfc) | 0x02; // 为什么怎么计算
					}
				}
				else 
				{
					warn_para.LowPress_time = 0;
					// TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmzhengchang_warn);
					CurrentPARA.warn_info = 0;
				}
				break;

			// case STATE_NORMAL://正常
			// 	if (CurrentPARA.air_pressure < (threshold - warn_deviation))
			// 	{
			// 		if (warn_para.LowPress_time == warn_LowPress_time)
			// 		{
			// 			currentState = STATE_LOW_PRESSURE;
			// 			TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmdiya_warn);
			// 		}
			// 	}
			// 	else 
			// 	{
			// 		warn_para.LowPress_time = 0;
			// 		TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmzhengchang_warn);
			// 	}
			// 	break;

			// case STATE_LOW_PRESSURE:
			// 	if (CurrentPARA.air_pressure > (threshold - warn_deviation)) 
			// 	{
			// 		currentState = STATE_NORMAL;
			// 		TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmzhengchang_warn);
			// 	} 
			// 	else 
			// 	{
			// 		TFTLCD_Draw_RLE_Bitmap(289, 13, (const GUI_BITMAP *)&bmdiya_warn);
			// 	}
			// 	break;
    	}
	}
}

/***************************adc_debug检测*******************/
void debug_adc()
{
	char ShowTemp1[32];
	if (debug_humidity)//湿度检测debug
	{
		sprintf(ShowTemp1, "humi   %04d", debug_humidity);
		TFTLCD_Str(230, 0, BRRED, BLACK, ShowTemp1, &Arial_18);
		debug_humidity = 0;
	}
	if (debug_PowerCheck)//电源检测debug
	{
		sprintf(ShowTemp1, "power %04d", debug_PowerCheck);
		TFTLCD_Str(230, 20, BRRED, BLACK, ShowTemp1, &Arial_18);
		debug_PowerCheck = 0;
	}
	if (debug_AirPress2)//空气压力检测debug
	{
		sprintf(ShowTemp1, "turn    %04d", debug_AirPress2);
		TFTLCD_Str(230, 40, BRRED, BLACK, ShowTemp1, &Arial_18);
		debug_AirPress2 = 0;
	}
}
